package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
   @Autowired
   DishMapper mapper;
   @Autowired
    DishFlavorMapper dishFlavorMapper;
   @Autowired
    SetmealDishMapper setmealDishMapper;
   @Autowired
    SetmealMapper setmealMapper;
   @Autowired
    SetmealDishService setmealDishService;
   @Autowired
    SetmealService setmealService;

    @Override
    @Transactional
    public void insert(DishDto dishDto) {
        int insert = mapper.insert(dishDto);
        //判断口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        if(flavors != null && flavors.size() > 0) {
            //保存菜品的口味数据 需要注意：DishFlavor中dishId是没有数据的，
            // 所以需要手动设置
            Long id = dishDto.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
                int insert1 = dishFlavorMapper.insert(flavor);

            }
        }
    }

    @Override
    public void delete(List<Long> ids) {

    //取出菜品id
        for (Long id : ids) {
         /*   //口味操作
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId,id);
            //查找口味
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(lqw);
            if(dishFlavors!=null){
                throw new CustomException("关联口味，不能删除");
            }else {
                dishFlavorMapper.deleteById(id);
            }*/
            LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Dish::getId,id);
            Dish dish = mapper.selectOne(lqw);
            Integer status = dish.getStatus();
            if(status == 1){
                throw  new CustomException("菜品正在销售，不能删除");
            }
            //套餐操作
            LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(SetmealDish::getDishId,id);
        //    查找关联套餐菜品，删除
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lqw1);
            if(setmealDishes.size() == 0){
                setmealDishService.remove(lqw1);
            }else {
                throw  new CustomException("关联套餐，不可删除");
            }
            //删除套餐
            for (SetmealDish setmealDish : setmealDishes) {
                Long setmealId = setmealDish.getSetmealId();
                setmealMapper.deleteById(setmealId);
            }
            //删除菜品和口味
            mapper.deleteById(id);
            dishFlavorMapper.deleteById(id);

        }
    }
}
